package com.soul.soulcommon.detail.service;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.soul.soulcommon.detail.OlderDetail;
import com.soul.soulcommon.dto.Jwt;
import com.soul.souldao.domain.Older;
import com.soul.souldao.mapper.OlderMapper;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.core.userdetails.UsernameNotFoundException;
import org.springframework.stereotype.Service;
@Slf4j
@Service
public class OlderDetailService implements UserDetailsService {
    @Autowired
    private OlderMapper olderMapper;
    @Override
    public UserDetails loadUserByUsername(String userName) throws UsernameNotFoundException {
        log.info("老人进行登录===========================");
        Older older = olderMapper.selectOne(new LambdaQueryWrapper<Older>().eq(Older::getUserName, userName));
        if(older == null){
            throw new UsernameNotFoundException("用户或密码不正确");
        }
        Jwt jwt = new Jwt(older.getId(), older.getUserName(), older.getPassword(), older.getRoleName());
        OlderDetail olderDetail = new OlderDetail(jwt);
        return olderDetail;
    }
}
